/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chess;

import javax.swing.ImageIcon;
import javax.swing.JButton;
/**
 *
 * @author Alejandro
 */
public class King extends Token{
    
    public King (int column, int row, int color, JButton [][] board,
            Token [][] tokens){
        ImageIcon[] iconos = {new ImageIcon("src/imagenes/Scourge/lich-K50.gif")
                        ,new ImageIcon("src/imagenes/Legion/leshrac-K50.gif")};
        this.setRow(row);
        this.setIcon(iconos);
        this.setColumn(column);
        this.setColor(color);
        this.board = board;
        this.setTokenOnBoard();
        this.tokens = tokens;
        this.setType("K");
        this.setPass(true);
    }

    @Override
    public void moveToken(int newColumn, int newRow){
        int F = this.getRow();
        int C = this.getColumn();
        boolean hit = tokens[newColumn][newRow] != null;
        enemy = hit && tokens[newColumn][newRow].getColor() != this.getColor();
        if ((((Math.abs(newRow - F) == Math.abs(newColumn - C)) && 
                (Math.abs(newColumn-C) == 1) && (Math.abs(newRow-F) == 1))
                ||  ((newRow == F && Math.abs(newColumn-C) == 1))  
                || ((newColumn == C)&& Math.abs(newRow-F) == 1))
                && (!hit || enemy)){
            board[C][F].setIcon(null);
            this.setRow(newRow);
            this.setPass(false);
            this.setColumn(newColumn);
            this.setTokenOnBoard();
            this.setCorrectMove(true);
        }
        else if (this.getPass() && newRow == F && ((newColumn == C + 3 && tokens[newColumn][newRow] != null && tokens[newColumn][newRow].getType().equals("R") &&tokens[newColumn][newRow].getPass()) 
                || (newColumn == C - 4 && tokens[newColumn][newRow] != null && tokens[newColumn][newRow].getType().equals("R") && tokens[newColumn][newRow].getPass()))){
            if (newColumn == C + 3 && tokens[C + 1][newRow] == null && tokens[C + 2][newRow] == null){
                board[C][F].setIcon(null);
                board[newColumn][newRow].setIcon(null);
                tokens[newColumn][newRow].setColumn(C + 1);
                tokens[C + 1][newRow] = tokens[newColumn][newRow];
                tokens[newColumn][newRow] = null;
                ImageIcon[] towers = {new ImageIcon("src/imagenes/Scourge/pudge-T50.gif")
                        ,new ImageIcon("src/imagenes/Legion/tiny-T50.gif")};
                board[C + 1][newRow].setIcon(towers[this.getColor()]);
                this.setRow(newRow);
                this.setPass(false);
                this.setColumn(C + 2);
                this.setTokenOnBoard();
                this.setCorrectMove(true);
            }
            else if (newColumn == C - 4 && tokens[C - 3][newRow] == null && tokens[C - 2][newRow] == null && tokens[C - 1][newRow] == null ){
                board[C][F].setIcon(null);
                board[newColumn][newRow].setIcon(null);
                tokens[newColumn][newRow].setColumn(C - 1);
                tokens[C - 1][newRow] = tokens[newColumn][newRow];
                tokens[newColumn][newRow] = null;
                ImageIcon[] towers = {new ImageIcon("src/imagenes/Scourge/pudge-T50.gif")
                        ,new ImageIcon("src/imagenes/Legion/tiny-T50.gif")};
                board[C - 1][newRow].setIcon(towers[this.getColor()]);
                this.setRow(newRow);
                this.setPass(false);
                this.setColumn(C -2);
                this.setTokenOnBoard();
                this.setCorrectMove(true);
            }
            else{
                this.setCorrectMove(false); 
            }
        }
        else{
            this.setCorrectMove(false);
            //mensaje de error
        }
    }
    
    private void setTokenOnBoard(){
        board[this.getColumn()][this.getRow()].setIcon(this.getIcon()[this.getColor()]);
    }
}
